package com.wl.servlets;

import com.wl.fruit.dao.FruitDAO;
import com.wl.fruit.dao.impl.FruitDAOImpl;
import com.wl.fruit.pojo.Fruit;
import com.wl.utils.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.List;

//@WebServlet("/fruit.do")
public class FruitServlet extends ViewBaseServlet{
    private FruitDAO fruitDAO = new FruitDAOImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        String operate = req.getParameter("operate");
        if (StringUtil.isEmpty(operate)) {
            operate = "index";
        }
        // 获取当前类所有的方法
        Method[] methods = this.getClass().getDeclaredMethods();
        for (Method method : methods) {
            // 获取方法名称
            String methodName = method.getName();
            if (operate.equals(methodName)) {
                try {
                    // 执行方法
                    method.invoke(this, req, resp);
                    return;
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }

        throw new RuntimeException("operate参数错误");

//        switch (operate) {
//            case "index":
//                index(req, resp);
//                break;
//            case "add":
//                add(req, resp);
//                break;
//            case "del":
//                del(req, resp);
//                break;
//            case "edit":
//                edit(req, resp);
//                break;
//            case "update":
//                update(req, resp);
//                break;
//            default:
//                throw new RuntimeException("operate参数错误");
//        }
    }

    private void index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Integer pageNo = 1;
        HttpSession session = req.getSession();

        String oper = req.getParameter("oper");
        String keyword = null;
        // 判断是否点击查询发送来的请求
        if (!StringUtil.isEmpty(oper) && "search".equalsIgnoreCase(oper)) {
            keyword = req.getParameter("keyword");
            if (StringUtil.isEmpty(keyword)) {
                keyword = "";
            }
            session.setAttribute("keyword", keyword);
        } else {
            String pageNoStr = req.getParameter("pageNo");
            if (!StringUtil.isEmpty(pageNoStr)) {
                pageNo = Integer.parseInt(pageNoStr);
            }
            Object keywordObj = session.getAttribute("keyword");
            if (keywordObj != null) {
                keyword = String.valueOf(keywordObj);
            } else {
                keyword = "";
            }
        }

        session.setAttribute("pageNo", pageNo);
        FruitDAO fruitDAO = new FruitDAOImpl();
        List<Fruit> fruitList = fruitDAO.getFruitList(keyword, pageNo);
        // 总记录数
        Long fruitCount = fruitDAO.getFruitCount(keyword);
        // 总条数
        Long pageCount = (fruitCount + 5 - 1) / 5;
        session.setAttribute("pageCount", pageCount);

        // 保存到session
        req.getSession().setAttribute("fruitList", fruitList);
        // 此处的视图名称为index
        // 那么thymeleaf会将这个逻辑视图名称对应到物理视图名称上
        // 逻辑视图: index 物理视图: view-prefix + 逻辑视图名称 + view-suffix
        // 真实视图名称 /index.html
        super.processTemplate("index", req, resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String fname = req.getParameter("fname");
        Integer price = Integer.parseInt(req.getParameter("price"));
        Integer fcount = Integer.parseInt(req.getParameter("fcount"));
        String remark = req.getParameter("remark");

        FruitDAO fruitDAO = new FruitDAOImpl();
        boolean flag = fruitDAO.addFruit(new Fruit(0, fname, price, fcount, remark));
        resp.sendRedirect("fruit.do");
    }

    private void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String fidStr = req.getParameter("fid");
        if (!StringUtil.isEmpty(fidStr)) {
            Integer fid = Integer.parseInt(fidStr);
            boolean flag = fruitDAO.delFruit(fid);
            resp.sendRedirect("fruit.do");
        }
    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String fidStr = req.getParameter("fid");
        if (!StringUtil.isEmpty(fidStr)) {
            int fid = Integer.parseInt(fidStr);
            Fruit fruit = fruitDAO.getFruitByFid(fid);
            req.setAttribute("fruit", fruit);
            super.processTemplate("edit", req, resp);
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer fid = Integer.parseInt(req.getParameter("fid"));
        String fname = req.getParameter("fname");
        Integer price = Integer.parseInt(req.getParameter("price"));
        Integer fcount = Integer.parseInt(req.getParameter("fcount"));
        String remark = req.getParameter("remark");
        boolean flag = fruitDAO.updateFruit(new Fruit(fid, fname, price, fcount, remark));
//        super.processTemplate("index", req, resp);
        // 重定向 目的是重新给indexServlet发请求 重新获取fruitList,然后覆盖到session中 这样index.html页面显示的session中数据才是最新的
        resp.sendRedirect("fruit.do");
    }
}
